fix truncateFilePath edge case on windows
authorJoey Hess <joeyh@joeyh.name>
Thu, 30 Jan 2025 20:51:42 +0000 (16:51 -0400)
committerJoey Hess <joeyh@joeyh.name>
Thu, 30 Jan 2025 20:53:10 +0000 (16:53 -0400)
If the filepath starts with something that is not valid utf-8, it would
have returned "". And if the filepath was all non-valid utf-8, it would
also return "".

Utility/FileSystemEncoding.hs

index b4497f30afcf51b2487125b2748c14ad765af045..cf9355ccd52f6d6d2516172edd0e7984ff24c3bf 100644 (file)
@@ -157,10 +157,13 @@ truncateFilePath n = toRawFilePath . reverse . go [] n
        go coll cnt bs
                | cnt <= 0 = coll
                | otherwise = case S8.decode bs of
-                       Just (c, x) | c /= S8.replacement_char ->
-                               let x' = fromIntegral x
-                               in if cnt - x' < 0
-                                       then coll
-                                       else go (c:coll) (cnt - x') (S8.drop 1 bs)
+                       Just (c, x)
+                               | c /= S8.replacement_char ->
+                                       let x' = fromIntegral x
+                                       in if cnt - x' < 0
+                                               then coll
+                                               else go (c:coll) (cnt - x') (S8.drop 1 bs)
+                               | otherwise ->
+                                       go ('_':coll) (cnt - 1) (S8.drop 1 bs)
                        _ -> coll
 #endif